<?php
namespace Home\Controller;

use Think\Controller;

class NewsController extends BaseController
{
    /**
     * 新闻列表页，默认显示第一个分类
     *
     */
    public function index()
    {
        //新闻显示
        $category_list = D("News")->table("dsk_news_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        $cat_id = I("get.cat_id", 0);
        $_wheresql = "";
        if (!empty($cat_id)) {
            $_wheresql = "cat_id = $cat_id";
        }

        $cat_info = array();
        if (!empty($cat_id)) {
            $cat_info = D("News")->table("dsk_news_category")->where("id = $cat_id")->find();
        }
        //  $news_list = D("News")->table("dsk_news")->where($_wheresql)->order(array('add_time'=>"desc"))->select();
        $count = D("News")->table("dsk_news")->where($_wheresql)->count();// 查询满足要求的总记录数 $map表示查询条件
        $rollPage = 8;// 分页栏每页显示的页数
        $offset = 15; //每页显示记录数
        $page = $this->set_pagination($count, $offset, $rollPage);
        // 进行分页数据查询
        $orderby['add_time'] = 'desc';
        $news_list = D("News")->table("dsk_news")->where($_wheresql)->order(array('add_time' => "desc"))->limit($page['firstRow'] . ',' . $page['listRows'])->select();
        $data = array(
            "category_list" => $category_list,
            "cat_id" => $cat_id,
            "news_list" => $news_list,
            "cat_info" => $cat_info,
            "menu" => "news",
            "page" => $page['page_str'],
            "news_count" => $count
        );
        $this->assign($data);
        $this->display();
    }

    public function info()
    {
        $id = I("get.id", 0);
        $cat_id = I("get.cat_id", 0);
        if (!empty($id)) {
            $news_info = D("News")->table("dsk_news")->where("id=$id")->find();
            $data = array("news_info" => $news_info);

        }
        $_wheresql = "cat_id = $cat_id";
        $cat_info = array();
        if (!empty($cat_id)) {
            $cat_info = D("News")->table("dsk_news_category")->where("id = $cat_id")->find();
        }
        $category_list = D("News")->table("dsk_news_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        $data['category_list'] = $category_list;
        $data['cat_info'] = $cat_info;
        $data['cat_id'] = $cat_id;
        $data['menu'] = "news";
        $this->assign($data);

        $this->display("news_detail");
    }
}